package blog.controller;

import blog.pojo.*;
import blog.service.*;
import blog.util.VerifyCode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.aspectj.weaver.ast.Not;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private ChapterCategoryService chapterCategoryService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private ChapterSpecialService chapterSpecialService;

    @Autowired
    private SupportService supportService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private ChapterLabelService chapterLabelService;

    @Autowired
    private NoticeCategoryService noticeCategoryService;

    @Autowired
    private ConfigService configService;

    @RequestMapping("/")
    public String autoIndex(Model model) throws Exception{
        return this.index(model);
    }

    @ModelAttribute
    private void copyright(Model model) throws Exception{
        String copyright =  configService.select(Config.COPYRIGHT);
        model.addAttribute("copyright", copyright);
    }

    @RequestMapping("/index")
    public String index(Model model) throws Exception {
        List<ChapterCategory> list = chapterCategoryService.getIndexList();
        model.addAttribute("categorylist", list);
        List<Chapter> chapterList = chapterService.getIndexCommendList();
        model.addAttribute("chapterList", chapterList);
        return "index";
    }

    @RequestMapping("/list")
    public String list(Model model, HttpServletRequest request) throws Exception{
        Integer page = request.getParameter("page")!=null ? Integer.parseInt(request.getParameter("page")) : 1;
        Integer categoryId = request.getParameter("categoryId")!=null?Integer.parseInt(request.getParameter("categoryId")):null;
        Integer labelId = request.getParameter("labelId")!=null?Integer.parseInt(request.getParameter("labelId")):null;


        Page<Chapter> chapterPage = chapterService.getPage(page, 15, categoryId, labelId);
        for(Chapter chapter: chapterPage.getRecords()){
            chapter.setCategoryList(chapterCategoryService.getListByChapterId(chapter.getId()));
            chapter.setLabelList(chapterLabelService.getListByChapterId(chapter.getId()));
        }


        model.addAttribute("page", chapterPage);
        List<ChapterCategory> list = chapterCategoryService.getIndexList();
        model.addAttribute("categorylist", list);

        return "list";
    }

    @RequestMapping("/info")
    public String info(Model model, HttpServletRequest request) throws Exception{
        String id = request.getParameter("id");
        if(id == null || "".equals(id)){
            //如果错误直接跳转首页
            return this.index(model);
        }

        Chapter chapter = chapterService.getInfo(Integer.valueOf(id));
        chapter.setLabelList(chapterLabelService.getListByChapterId(chapter.getId()));
        chapter.setCategoryList(chapterCategoryService.getListByChapterId(chapter.getId()));
        model.addAttribute("chapter", chapter);
        return "info";
    }

    @RequestMapping("/feedback")
    public String feedback(Model model, HttpServletRequest request, HttpSession httpSession) throws Exception{
        model.addAttribute("insert", 0);
        if("POST".equals(request.getMethod())){
            //验证码错误
            if(!httpSession.getAttribute("code").equals(request.getParameter("code"))){
                model.addAttribute("insert", 3);
            } else {
                Feedback feedback = new Feedback()
                        .setTitle(request.getParameter("title"))
                        .setContent(request.getParameter("content"))
                        .setSender(request.getParameter("sender"));
                if(feedbackService.insert(feedback)){
                    model.addAttribute("insert", 1);
                }else{
                    model.addAttribute("insert", 2);
                }
            }
        }
        return "feedback";
    }
    @RequestMapping("/noticeList")
    public String noticeList(Model model, HttpServletRequest request) throws Exception{
        int page = request.getParameter("page") != null ? Integer.parseInt(request.getParameter("page")) : 0;
        IPage<Notice> noticePage = noticeService.getPage(page, 15, null);
        model.addAttribute("page", noticePage);
        return "noticeList";
    }
    @RequestMapping("/notice")
    public String notice(Model model, HttpServletRequest request) throws Exception{
        if(request.getParameter("id") == null){
            return "redirect:noticeList";
        }
        Notice notice = noticeService.getInfo(Integer.valueOf(request.getParameter("id")));
        if(notice == null) {
            return "redirect:noticeList";
        }
        notice.setCategory(noticeCategoryService.selectById(notice.getCategoryId()));
        model.addAttribute("notice", notice);
        return "notice";
    }
    @RequestMapping("/about")
    public String about(Model model) throws Exception{
        ChapterSpecial chapterSpecial = chapterSpecialService.selectByName("about_me");
        model.addAttribute("chapterSpecial", chapterSpecial);
        return "about";
    }
    @RequestMapping("/support")
    public String support(Model model) throws Exception{
        List<Support> supports = supportService.selectAll();
        model.addAttribute("supportList", supports);
        return "support";
    }

}

